<?php

namespace app\admin\controller;

use app\admin\model\CertTemplate as Model;
use app\admin\model\store\User;
use app\admin\model\web\Feedback;
use app\common\model\job\Job;
use app\admin\model\student\Sign;
use think\db\exception\DbException;
use think\facade\Db;

class Home extends Controller
{
    /**
     * 统计数据
     * @return array
     * @throws DbException
     */
    public function statistics()
    {
        $sign = Db::query("SELECT job_id, COUNT(*) as job_count FROM x_student_sign WHERE is_delete = 0 GROUP BY job_id ORDER BY COUNT(*) DESC LIMIT 1;");
        $ljzdgw_name = '';
        $ljzdgw_number = 0;
        if (!empty($sign[0])) {
            $ljzdgw_name = (new Job())->where(['job_id' => $sign[0]['job_id']])->value('job_name');
            $ljzdgw_number = $sign[0]['job_count'];
        }

        $sign = Db::query("SELECT job_id, COUNT(*) as job_count FROM x_student_sign WHERE is_delete = 0 GROUP BY job_id ORDER BY COUNT(*) asc LIMIT 1;");
        $ljzsgw_name = '';
        $ljzsgw_number = 0;
        if (!empty($sign[0])) {
            $ljzsgw_name = (new Job())->where(['job_id' => $sign[0]['job_id']])->value('job_name');
            $ljzsgw_number = $sign[0]['job_count'];
        }

        $answer = Db::query("SELECT title, COUNT(*) as count
FROM x_student_sign_answer_sheet
WHERE create_time >= UNIX_TIMESTAMP(NOW() - INTERVAL 15 DAY)
GROUP BY title
ORDER BY count DESC
LIMIT 1;");
        $day15_name = '';
        $day15_number = '';
        if (!empty($answer[0])) {
            $day15_name = $answer[0]['title'];
            $day15_number = $answer[0]['count'];
        }
        $pastDate = '2025-05-20'; // 已知的过去日期
        $today = new \DateTime(); // 当前日期
        $targetDate = new \DateTime($pastDate);
        $interval = $today->diff($targetDate);
        $daysPassed = $interval->days; // 相差的天数
        $fwcs = Db::name('admin_setting')->where([
            'key' => 'web_certificate_query'
        ])->value('values');
        //
        $data = [
            'xtxx' => [
                'yxts' => $daysPassed,
                'fwcs' => $fwcs,
                'jrbm' => (new Sign())->whereTime('create_time', 'today')->where(['is_delete' => 0])->count(),
            ],
            'dclrw' => [
                'dsczs' => (new Sign())->where(['is_delete' => 0, 'status' => 1])->count(),
                'ddyzs' => (new Sign())->where(['is_delete' => 0, 'status' => 3])->count(),
                'fkwcl' => (new Feedback())->where(['is_delete' => 0, 'status' => 0])->count(),
            ],
            'bmxytj' => [
                'xyzsl' => (new Sign())->where(['is_delete' => 0])->count(),
                'zrbmsl' => (new Sign())->whereTime('create_time', 'yesterday')->where(['is_delete' => 0])->count(),
                'yznbmsl' => (new Sign())->whereTime('create_time', 'week')->where(['is_delete' => 0])->count(),
            ],
            'bmgwfx' => [
                'ljzdgw' => [
                    'name' => $ljzdgw_name,
                    'number' => $ljzdgw_number
                ],
                'ljzsgw' => [
                    'name' => $ljzsgw_name,
                    'number' => $ljzsgw_number
                ],
                'day15' => [
                    'name' => $day15_name,
                    'number' => $day15_number
                ]
            ]
        ];
        return $this->renderSuccess($data);
    }

    public function charts($type, $times)
    {
        $times = between_time2($times);
        if ($type == 1) {
            $sign = (new Sign())
                ->field('student_sign_id,job_level,channel_name')
                ->where(['is_delete' => 0])
                ->group('channel_name')
                ->select();
        } else {
            $sign = (new Sign())
                ->field('student_sign_id,job_level,store_user_id')
                ->where(['is_delete' => 0])
                ->group('store_user_id')
                ->select();
        }
        $count_number = 0;
        foreach ($sign as $item) {
            if ($type == 1) {
                $field_name = 'channel_name';
                $field_value = $item->channel_name;
                $item->charts_name = $field_value ?: '无';
            } else {
                $field_name = 'store_user_id';
                $field_value = $item->store_user_id;
                $item->charts_name = (new User)->where(['store_user_id' => $field_value])->value('real_name');
            }
            $item->count_number = (new Sign())
                ->where($field_name, '=', $field_value)
                ->where('create_time', '>=', $times['start_time'])
                ->where('create_time', '<=', $times['end_time'])
                ->where('is_delete', '=', 0)
                ->count();

            $count_number += $item->count_number;
        }
        $sign = $sign->toArray();
        $age = array_column($sign, 'count_number');
        array_multisort($age, SORT_DESC, $sign);

        return $this->renderSuccess(['sign' => $sign, 'count_number' => $count_number]);
    }
}
